package com.gdut.simple.presenter.user;

import com.gdut.simple.R;
import com.gdut.simple.basic.BasicApplication;
import com.gdut.simple.common.NetworkUtils;
import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.contract.LoginContract;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.model.ILoginModel;
import com.gdut.simple.model.impl.LoginModelImpl;

public class LoginPresenter implements LoginContract.Presenter {

    private LoginContract.View mLoginView;
    private ILoginModel mLoginModel;

    public LoginPresenter(LoginContract.View view) {
        mLoginView = view;
        mLoginModel = new LoginModelImpl();
        mLoginView.setPresenter(this);
    }

    @Override
    public void login() {
        if (!NetworkUtils.isConnected(BasicApplication.getApplication())) {
            mLoginView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_error_net));
            return;
        }
        String account = mLoginView.getAccount();
        String password = mLoginView.getPassword();
        if (account.isEmpty() || password.isEmpty()) {
            mLoginView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_null_acc_psw));
            return;
        }
        mLoginView.showProgress(BasicApplication.getApplication().getResources().getString(R.string.msg_login));
        mLoginModel.login(account, password, new RequestCallback<User>() {

            @Override
            public void onSuccess(User user) {
                mLoginView.hideProgress();
                mLoginView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_login_success));
                mLoginView.next();
            }

            @Override
            public void onFailure(String message) {
                mLoginView.hideProgress();
                mLoginView.showMessage(message);
            }
        });
    }

}
